﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using COM.Data;
using COM.Service;
using COM.Web;
using WebApp.Data;
using WebApp.Service;

namespace WebApp.Web.Service
{
    public interface IAppLoginService : ILoginService
    {
        bool LoginAsTempFileUser(string userCodeOrName, string password);
    }

    public class AppLoginService : LoginService, IAppLoginService
    {
        private IAppUserPrincipalFactory appUserPrincipalFactory;
        private IUserService UserService;
        private IWebPrincipalService webPrincipalService;
        public AppLoginService(IUserService UserService, IAppUserPrincipalFactory appUserPrincipalFactory, IWebConfigService configService, IWebPrincipalService webPrincipalService)
            : base(appUserPrincipalFactory, configService, webPrincipalService)
        {
            this.UserService = UserService;
            this.appUserPrincipalFactory = appUserPrincipalFactory;
            this.webPrincipalService = webPrincipalService;
        }

        public bool LoginAsTempFileUser(string userCodeOrName, string password)
        {
            if (this.UserService.CheckUserPassword(userCodeOrName, password))
            {
                var user = this.UserService.GetUserByCodeOrName(userCodeOrName);
                var p = this.appUserPrincipalFactory.CreateMvcUserPrincipal(user.ID, user.UserCode);
                this.webPrincipalService.SignIn(p);
                return true;
            }
            return false;
        }


    }
}